﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProtocolBuilder.Model.Field
{
    class LoopField : ComposeField
    {
        public override string GenerateSerializeCode()
        {
            string prefix = this.GenerateWhiteSpace();
            int generation = this.GetGeneration();
            string result = prefix;

            result += string.Format("for(int i{0} = 0;i{0} < @p1;i{0}++)", generation == 0 ? string.Empty : generation.ToString());
            result += TextHelper.HtmlNewLine;
            result += string.Format("{0}{{", prefix);
            result += TextHelper.HtmlNewLine;
            foreach (AbsField child in this.GetChildrenField())
            {
                result += child.GenerateSerializeCode();
                result += TextHelper.HtmlNewLine;
            }
            result += string.Format("{0}}}", prefix);


            return result;
        }

        public override string GenerateDeserializeCode()
        {
            string prefix = this.GenerateWhiteSpace();
            int generation = this.GetGeneration();
            string result = prefix;

            result += string.Format("for(int i{0} = 0;i{0} < @p1;i{0}++)", generation == 0 ? string.Empty : generation.ToString());
            result += TextHelper.HtmlNewLine;
            result += string.Format("{0}{{", prefix);
            result += TextHelper.HtmlNewLine;
            foreach (AbsField child in this.GetChildrenField())
            {
                result += child.GenerateDeserializeCode();
                result += TextHelper.HtmlNewLine;
            }
            result += string.Format("{0}}}", prefix);


            return result;
        }
    }
}
